class Account::BalancesController < Account::BaseController

  def show
    # Laat een view zien
  end

  def edit
    @creditcard = Checkout::Payment::Creditcard.new
    @creditcard_companies = Checkout::Payment::Creditcard.valid_companies
  end

  def update
    @creditcard = Checkout::Payment::Creditcard.new(params[:checkout_payment_creditcard])
    @creditcard_companies = Checkout::Payment::Creditcard.valid_companies

    if params[:amount].to_i <= 0
      @creditcard.errors.add_to_base('U heeft geen geldig bedrag ingevoerd.')

      render :action => 'edit'
    elsif !@creditcard.valid?
      render :action => 'edit'
    else

      begin
        @creditcard.execute(params[:amount].to_i)
        flash[:notice] = "Het geld is op uw rekening gestort."

        @account.balance += params[:amount].to_i
        @account.save

        redirect_to account_balance_path
      rescue Checkout::Payment::PaymentFailedException => e
        flash[:notice] = "##{e.code} #{e.message}"
      end
    end

  end

end